import { loginApi, getUserInfoApi } from '@/api/user'
import { getToken, setToken, removeToken, setUserInfo } from '@/utils/auth'
import router from '@/router'
import { Message } from 'element-ui'
// import axios from 'axios'
// import service from '@/utils/request'

export default {
  namespaced: true,
  state() {
    return {
      token: getToken() || ''
      // username: getUserInfo().username || '',
      // password: getUserInfo().password || '',
      // uuid: getUserInfo().uuid || ''
    }
  },
  mutations: {
    // 1.1存储token
    SET_TOKEN: (state, token) => {
      // token存储到本地
      setToken(token)
      // 1.2存储token到vuex
      state.token = token
    },
    // 1.3删除token
    REMOVE_TOKEN(state) {
      removeToken()
      state.token = null
    },
    // 2.1存储用户名
    SET_INFO(state, { username, password, uuid }) {
      state.username = username
      state.password = password
      state.uuid = uuid
      setUserInfo({ username, password, uuid })
    }
  },
  actions: {
    // 登录
    async login(context, { username, password }) {
      const res = await loginApi({ username, password })
      // console.log(token)
      context.commit('SET_TOKEN', res.data.token)
      Message.success('登陆成功~')
      // 1.2跳转到首页
      router.replace('/')
    },
    // 2.退出登录
    logout(context) {
      context.commit('REMOVE_TOKEN') // 清除token
    },
    // 3.获取用户信息
    async getUserInfo(context) {
      const usernameInfo = await getUserInfoApi()
      console.log(usernameInfo)
    }
  }
}
